
#include "main.h"
//#include "stm32f10x_conf.h"
//#include "hal_digitalin.h"
//#include "hal_eeprom.h"
//#include "mb_config.h"
//#include "mbvardef.h"
#include "keypad.h"
#include "hal_7seg.h"
#include "Timer.h"
#include "IDriver.h"
#include "Control.h"
#include "ModbusRegister.h"
#include "Register.h"

#define KEY_IDLE        0
#define KEY_IR_BROWSE   1
#define KEY_HR_BROWSE   2
#define KEY_HR_SET      3
#define KEY_HR_PASSWD   4

#define KEY_SET   0x01
#define KEY_UP    0x02
#define KEY_DN    0x04

extern uint8_t init_delay;
//static uint16_t Update;
static uint32_t Update,Timers100msUpdate,TimersSecondUpdate;
//extern DELAY delay_keypad_set;
//extern DELAY delay_twinkle;
//uint8_t flag_twinkle;

uint8_t Key_Task_ID;
uint8_t Key_Var_Number;
int16_t Keypad_SetValue;
int16_t Keypad_SetDelta;
uint8_t Keypad_21_index;
uint8_t Keypad_22_index;

uint8_t Keypad_tick = 0;
uint8_t Keypad_Idle_Stage = 0;

uint8_t error_display = 0;

//static MTIMER tm_Keypad;

uint8_t key_kept = 0;
static uint8_t factory_test,factory_test_stage,factory_test_error;

const uint8_t ir_params_define[IR_VAR_DISP_MAX]=
{
  0, //IR_DI_CMP_RUN_ST    0      //压缩机运行
  0, //IR_DI_LO_PRESS_ST  1      //低压开关
  0, //IR_DI_HI_PRESS_ST  2      //高压开关
  0, //IR_DI_DEFROST_ST    3      //除霜模式
  0, //IR_DI_REMOTE_ON_ST  4      //远程启停
  1, //IR_TEMP_DISCHG      5      //排气温度， Td  
  1, //IR_TEMP_SUCTION    6      //吸气温度， Ts
  1, //IR_TEMP_OUTDOOR    7    //室外温度， To
  2, //IR_PRESS_SUCTION    8    //低压压力， Ps
  2, //IR_PRESS_DISCHG    9    //高压压力， Pd
  0, //IR_DO_FAN_SS_1      10    //风扇1，启停
  0, //IR_DO_FAN_SS_2      11    //风扇2，启停
  0, //IR_DO_CMPR_HTR      12    //压缩机加热带，启停
  0, //IR_TO_EEV_SET      13    //电子膨胀阀控制
  1, //IR_TEMP_EVAPORATION    14  //蒸发温度， Te
  1, //IR_TEMP_CONDENSITION    15  //冷凝温度， Tc
  1, //IR_TdSH                16  //排气过热度， TdSH
  1, //IR_TsSH                17  //吸气过热度， TsSH
	0, //18
  1, //IR_CMPR_START          19  //压缩机启停控制
  0, //IR_CMPR_FRQ            20  //压缩机频率
  0, //IR_ERROR_CODE          21  //故障代码 
	0, //22
	0, //23
	0, //24
	0, //25
	0, //26
	0, //27
	0, //28
	0, //29
	0, //30
	0, //31
	0, //32
	0, //33
	0, //34
	0, //35
	0, //36
	0, //37
	0, //38
	0, //39
	0, //40
	0, //41
	1, //42
	0, //43
	0, //44
	0, //45
	1 //46
};


uint8_t HAL_DI_GetKeypad(void)
{
	uint8_t key_in;
	
	key_in= (GPIOC->IDR & 0x0038) >> 3;
		
	return ((~key_in)&0x07);   // 1 2  4
}

uint8_t getNextHistoryErr(uint8_t errIndex)
{
  return InputReg[REG_ERROR_RECORD_1+errIndex];
}

uint8_t getLastHistoryErr(uint8_t errIndex)
{
  return InputReg[REG_ERROR_RECORD_1];;
}


uint8_t KEYPAD_Control(void )
{
  uint8_t key_in, key;
  static uint8_t init= 0;
  static uint8_t last_key= 0;
  static uint8_t key_same_cnt= 0;
  static uint8_t last_key_in= 0;
  uint8_t TicksKeypad100ms = 0;
	uint8_t TicksKeypadSecond = 0;
  u32 TicksNow, TicksElapsed;
    
    TicksNow = Timer2_Get32Ticks();
    TicksElapsed = TicksNow - Update;
    
    if(TicksElapsed < 2)
    {
            return  0;
    }
		Update = Timer2_Get32Ticks();
		
		TicksElapsed = TicksNow - Timers100msUpdate;
		if(TicksElapsed > 100)
		{
			
			Timers100msUpdate = Timer2_Get32Ticks();		
			TicksKeypad100ms = 1;	
		}
	
		TicksElapsed = TicksNow - TimersSecondUpdate;
		if(TicksElapsed > 1000)
		{
			
			TimersSecondUpdate = Timer2_Get32Ticks();		
			TicksKeypadSecond = 1;	
		}
	
  if (init_delay > 3) 
	{
    HAL_7Seg_SetData(SOFTWARE_VERSION,2); // VERSION
    HAL_7Seg_Display();
    return 0;
	}
   else if (init_delay > 0) 
  {
    HAL_7Seg_SetText(CHARACTOR_C,CHARACTOR_M,CHARACTOR_U);
    HAL_7Seg_Display();
    return 0;
  }
	
	
		

  if(!init)
  {
    init= 1;
//    Timer_Init(&tm_Keypad, 1, 100);
//    Timer_Start(&tm_Keypad);
  }
  
  if (TicksKeypadSecond)
  {
    if (Keypad_tick < 100)
    {
      Keypad_tick++;
    }
    else
    {
      Keypad_tick--;
    }
  }
  
  if(TicksKeypad100ms == 1)
  {
    key_in= HAL_DI_GetKeypad();	
		
    if(key_in== last_key_in)
    {
      key= key_in;
    }
    else
    {
      key= 0;
    }
    
    if(key== KEY_SET && last_key== key)
    {
      key= 0;
    }
    
		factory_test = GetFactoryTest();
    if (factory_test)
    {
    }
    else
    switch(Key_Task_ID)
    {
      case KEY_IDLE:
        switch(key)
        {
          case KEY_SET:
            Key_Task_ID= KEY_HR_PASSWD;
            Keypad_SetValue= 0;
            Key_Var_Number= 0;
            Keypad_tick = 0;
            break;
          case KEY_UP:
          case KEY_DN:
            Key_Task_ID= KEY_IR_BROWSE;
            Key_Var_Number= 0;
            Keypad_tick = 0;
            break;
          default:
            break;
        }
        break;
      case KEY_HR_PASSWD:
        switch(key)
        {
          case KEY_SET:
            if (Keypad_SetValue == 3)
            {
              Key_Task_ID= KEY_HR_BROWSE;
              Key_Var_Number= 0;
            }
            else
            {
              Key_Task_ID= KEY_IDLE;
              Key_Var_Number= 0;
            }
            Keypad_tick = 0;
            break;
          case KEY_UP:
            Keypad_SetValue++;
            Keypad_tick = 0;
            break;
          case KEY_DN:
            Keypad_SetValue--;
            Keypad_tick = 0;
            break;
          default:
            break;
        }
        if (Keypad_tick >= 30)
        {
          Key_Task_ID= KEY_IDLE;
          Key_Var_Number= 0;
          Keypad_Idle_Stage= 0;
        }
        break;
      case KEY_IR_BROWSE:
        switch(key)
        {
          case KEY_SET:
						Key_Task_ID= KEY_IDLE;
						Key_Var_Number= 0;
            Keypad_tick = 0;
            break;
          case KEY_UP:
						Key_Var_Number++;
						if(Key_Var_Number > IR_VAR_DISP_MAX-1)
						{
							Key_Var_Number= IR_VAR_DISP_MAX-1;
						}
            Keypad_tick = 0;
            break;
          case KEY_DN:         
						if(Key_Var_Number)
						{
							Key_Var_Number--;
						}
            Keypad_tick = 0;
            break;
          default:
            break;
        }
        if (Keypad_tick >= 60)
        {
          Key_Task_ID= KEY_IDLE;
          Key_Var_Number= 0;
          Keypad_Idle_Stage= 0;
        }
        break;
      case KEY_HR_BROWSE:
        switch(key)
        {
          case KEY_SET:
            if (Key_Var_Number != HR_VAR_DISP_MAX)
            {
              Key_Task_ID= KEY_HR_SET;
            
              Keypad_SetValue= GetJDASDParamValue(Key_Var_Number);//HoldingReg[Key_Var_Number];
            }
            else
            {
							SetParamRegisterValue(Key_Var_Number, Keypad_SetValue);
              Key_Task_ID= KEY_IDLE;
              Keypad_Idle_Stage= 0;
            }
            Keypad_tick = 0;
            break;
          case KEY_UP:
            if(Key_Var_Number < HR_VAR_DISP_MAX)
            {
              Key_Var_Number++;
            }
            Keypad_tick = 0;
            break;
          case KEY_DN:
            if(Key_Var_Number)
            {
              Key_Var_Number--;
            }
            Keypad_tick = 0;
            break;
          default:
            break;
        }
        if (Keypad_tick >= 30)
        {
          Key_Task_ID= KEY_IDLE;
          Key_Var_Number= 0;
          Keypad_Idle_Stage= 0;
        }
        break;
      case KEY_HR_SET:
        switch(key)
        {
          case KEY_SET:
            Key_Task_ID= KEY_HR_BROWSE;
					  SetParamRegisterValue(Key_Var_Number, Keypad_SetValue);
            Keypad_tick = 0;
            break;
          case KEY_UP:
            if(key== last_key)
            {
              key_same_cnt++;
              
              if(key_same_cnt > 10 && (Keypad_SetValue%Param_Get_Delta(Key_Var_Number)*10)== 0)
              {
                Keypad_SetDelta= Param_Get_Delta(Key_Var_Number)*10;
              }
            }
            else
            {
              key_same_cnt= 0;
              Keypad_SetDelta= Param_Get_Delta(Key_Var_Number);
            }
            //
            Keypad_SetValue+= Keypad_SetDelta;
          
            if (Keypad_SetValue > Param_Get_MAX(Key_Var_Number))
            {
              Keypad_SetValue= Param_Get_MAX(Key_Var_Number);
            }
            Keypad_tick = 0;
            break;
          case KEY_DN:
            if(key== last_key)
            {
              key_same_cnt++;
              
              if(key_same_cnt > 10 && (Keypad_SetValue%Param_Get_Delta(Key_Var_Number)*10)== 0)
              {
                Keypad_SetDelta= Param_Get_Delta(Key_Var_Number)*10;
              }
            }
            else
            {
              key_same_cnt= 0;
              Keypad_SetDelta= Param_Get_Delta(Key_Var_Number);
            }
            
            if (Keypad_SetValue > Param_Get_MIN(Key_Var_Number))
            {
              if(Keypad_SetValue > Param_Get_MIN(Key_Var_Number) + Keypad_SetDelta)
              {
                Keypad_SetValue-=  Keypad_SetDelta;
              }
              else
              {
                Keypad_SetValue= Param_Get_MIN(Key_Var_Number);
              }
            }
            Keypad_tick = 0;
            break;
          default:
            break;
        }
        if (Keypad_tick >= 30)
        {
          Key_Task_ID= KEY_IDLE;
          Key_Var_Number= 0;
          Keypad_Idle_Stage= 0;
        }
        break;
      default:
        break;
    }
  
    last_key= key;
    last_key_in= key_in;
  }
  ///////////////////////////////////////////////////////////////////////////
  if (factory_test)
  {
		factory_test_stage = Getfactory_test_stage();
    switch(factory_test_stage)
    {
      case 0: // 排气温度
        HAL_7Seg_SetIndex(CHARACTOR_A | sp, InputReg[REG_TEMP_DISCHG] / 10);
        break;
      case 1: // 吸气温度
        HAL_7Seg_SetIndex(CHARACTOR_B | sp, InputReg[REG_TEMP_SUCTION] / 10);
        break;
      case 2: // 环境温度
        HAL_7Seg_SetIndex(CHARACTOR_C | sp, InputReg[REG_TEMP_OUTDOOR] / 10);
        break;
      case 3: // 排气压力
        HAL_7Seg_SetIndex(CHARACTOR_D | sp, InputReg[REG_PRESS_SUCTION] / 10);
        break;
      case 4: // 吸气压力
        HAL_7Seg_SetIndex(CHARACTOR_E | sp, InputReg[REG_PRESS_DISCHG] / 10);
        break;
      case 5: // DI输入
        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_I | sp, 0);
        break;
      case 7: // DO1
        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_1);
        break;
      case 8: // DO2
        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_2);
        break;
      case 9: // DO3
        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_3);
        break;
      case 10: // DO4
        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_4);
        break;
      case 11: // DO5
        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_5);
        break;
//      case 12: // DO6
//        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_6);
//        break;
//      case 13: // DO7
//        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_7);
//        break;
//      case 14: // DO8
//        HAL_7Seg_SetText(CHARACTOR_D, CHARACTOR_O | sp, CHARACTOR_8);
//        break;
//      case 15: // DO9
//        HAL_7Seg_SetText(CHARACTOR_T, CHARACTOR_O | sp, CHARACTOR_1);
//        break;
//      case 16: // DO10
//        HAL_7Seg_SetText(CHARACTOR_T, CHARACTOR_O | sp, CHARACTOR_2);
//        break;
      case 17: // DO6
        HAL_7Seg_SetText(CHARACTOR_T, CHARACTOR_O | sp, CHARACTOR_1);
        break;
      case 18: // 压缩机
        HAL_7Seg_SetIndex(CHARACTOR_Y | sp, 15);
        break;
      case 19: // 风机1
        HAL_7Seg_SetText(CHARACTOR_F, CHARACTOR_1 | sp, CHARACTOR_O);
        break;
      case 20: // 风机2
        HAL_7Seg_SetText(CHARACTOR_F, CHARACTOR_2 | sp, CHARACTOR_O);
        break;
      case 21: // 结束
      case 127:
        HAL_7Seg_SetText(0, CHARACTOR_O, CHARACTOR_K);
        break;
      case 255: // 错误
				factory_test_error = Getfactory_test_error();
        switch (factory_test_error)
        {
          case 1:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_A);
            break;
          case 2:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_B);
            break;
          case 3:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_C);
            break;
          case 4:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_D);
            break;
          case 5:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_E);
            break;
          case 6:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_1);
            break;
          case 7:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_3);
            break;
          case 8:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_6);
            break;
          case 9:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_Y);
            break;
          case 10:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_F);
            break;
          case 11:
            HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_r | sp, CHARACTOR_F | sp);
            break;
        }
        break;
    }
  }
  else if(Key_Task_ID== KEY_IDLE)
  {
    switch (Keypad_Idle_Stage)
    {
      case 0:
        HAL_7Seg_SetText(CHARACTOR_SPACE, CHARACTOR_F, CHARACTOR_SPACE);
        if (Keypad_tick >= 1)
        {
          Keypad_tick = 0;
          Keypad_Idle_Stage = 1;
        }
        break;
      case 1:
        HAL_7Seg_SetData(InputReg[REG_TD_RPS],0);
        if (Keypad_tick >= 3)
        {
          Keypad_tick = 0;
          Keypad_Idle_Stage = 2;
        }
        break;
      case 2:
        HAL_7Seg_SetText(CHARACTOR_SPACE, CHARACTOR_L, CHARACTOR_SPACE);
        if (Keypad_tick >= 1)
        {
          Keypad_tick = 0;
          Keypad_Idle_Stage = 3;
        }
        break;
      case 3:
        HAL_7Seg_SetData(InputReg[REG_TEMP_EVAPORATION],1);
        if (Keypad_tick >= 3)
        {
          Keypad_tick = 0;
          Keypad_Idle_Stage = 4;
        }
        break;
      case 4:
        HAL_7Seg_SetText(CHARACTOR_SPACE, CHARACTOR_H, CHARACTOR_SPACE);
        if (Keypad_tick >= 1)
        {
          Keypad_tick = 0;
          Keypad_Idle_Stage = 5;
        }
        break;
      case 5:
        HAL_7Seg_SetData(InputReg[REG_TEMP_CONDENSITION],1);
        if (Keypad_tick >= 3)
        {
          Keypad_tick = 0;
          if (InputReg[REG_ERROR_CODE])
          {
            error_display = getNextError();
            if (error_display == 0)
            {
              HAL_7Seg_SetIndex(CHARACTOR_E, InputReg[REG_ERROR_CODE]);
            }
            else
            {
              HAL_7Seg_SetIndex(CHARACTOR_E, error_display);
            }
            Keypad_Idle_Stage = 6;
          }
          else
          {
            Keypad_Idle_Stage = 0;
          }
        }
        break;
      case 6:
        if (Keypad_tick >= 4)
        {
          Keypad_tick = 0;
          Keypad_Idle_Stage = 0;
        }
        break;
      default:
        Keypad_Idle_Stage = 0;
        Keypad_tick = 0;
        break;
    }
  }
  else 
  {
    if(Key_Task_ID== KEY_IR_BROWSE)
    {
      if(Keypad_tick & 1)
      {
				HAL_7Seg_SetIndex(CHARACTOR_N, Key_Var_Number);
      }
      else 
      {
        if(key_in== 0) 
        {
          HAL_7Seg_SetData(InputReg[Key_Var_Number], ir_params_define[Key_Var_Number]);
        }
      }
    }
    else
    {
      if(Key_Task_ID== KEY_HR_BROWSE)
      {
        if (Key_Var_Number != HR_VAR_DISP_MAX)
        {
          HAL_7Seg_SetIndex(CHARACTOR_P, Key_Var_Number);
        }
        else
        {
          HAL_7Seg_SetText(CHARACTOR_E, CHARACTOR_N, CHARACTOR_D);
        }
      }
      else
      {
        if(Key_Task_ID== KEY_HR_SET)
        {
          HAL_7Seg_SetData(Keypad_SetValue, Param_Get_Decimal(Key_Var_Number));//hr_params_define[Key_Var_Number].decimal);
        }
        else
        {
          if (Key_Task_ID== KEY_HR_PASSWD)
          {
            if (Keypad_tick & 1)
            {
              HAL_7Seg_SetData(Keypad_SetValue, 0);
            }
            else
            {
              HAL_7Seg_SetText(CHARACTOR_P, CHARACTOR_W, CHARACTOR_SPACE);
            }
          }
        }
      }
    }
  }
  
  HAL_7Seg_Display();
	
	return 0;
}



void KeyPad_Init(void)
{
		
}
